// 序列化参数
export function serializeParams(obj) {
  const arr = [];
  let str = "";
  Object.keys(obj).forEach((item) => {
    arr.push(item + "=" + obj[item]);
  });
  str = arr.join("&");
  return str;
}
// 匹配字典
export function matchDictList(val, list = [], dictValue, dictLabel) {
  const find = list.find((item) => {
    return item[dictValue] == val;
  });
  if (!find) {
    return ""
  }
  return find[dictLabel]
}
// 验证表单是否已填
/**
 * 
 * @param {Object} formData 表单数据
 * @param {Object} rules 规则数据 
 * @param {String} rulesKey 规则对应的key
 * @param {Object} that this
 */
export function checkForm(formData, rules,rulesKey,that) {
  let rulesTemp = {
    ...rules
  };
  Object.keys(rules).forEach((key) => {
    if (rules[key].require && (!formData[key])) {
      rulesTemp[key].errMsg = rulesTemp[key].msg;
    }
    if (rules[key].require && formData[key]) {
      rulesTemp[key].errMsg = "";
    }
    if (rules[key].validator) {
      const {
        msg,
        bool
      } = rules[key].validator(formData[key]);
      if (!bool) {
        rulesTemp[key].errMsg = msg;
      } else {
        rulesTemp[key].errMsg = ""
      }
    }
  });
  that.setData({
    [rulesKey]: rulesTemp
  });
  let flag = true;
  Object.keys(rulesTemp).forEach((item) => {
    if (rulesTemp[item].errMsg != "") {
      flag = false;
    }
  });
  return flag;
}
  // 对部分字段校验
export function validateField(formData,rules,rulesKey,key,that){
    let rulesTemp = {
      ...rules
    };
    if(rules[key].require && (!formData[key])){
      rulesTemp[key].errMsg = rulesTemp[key].msg;
    }
    if(rules[key].require && formData[key]){
      rulesTemp[key].errMsg = "";
    }
    if(rules[key].validator){
      const {msg,bool} = rules[key].validator(formData[key]);
      if(!bool){
        rulesTemp[key].errMsg = msg;
      }else{
        rulesTemp[key].errMsg = ""
      }
    }
    that.setData({
      [`${rulesKey}.${key}.errMsg`]:rulesTemp[key].errMsg
    })
  }